package com.yanqu.road.server.pb;

import com.yanqu.road.entity.config.system.PropertyWeightItem;
import com.yanqu.road.entity.enums.kuang.eKuangAttributeType;
import com.yanqu.road.entity.kuang.KuangEquipmentData;
import com.yanqu.road.entity.kuang.KuangRankRewardConfig;
import com.yanqu.road.entity.kuang.KuangUserData;
import com.yanqu.road.entity.kuang.group.KuangMatchGroup;
import com.yanqu.road.pb.kuang.Kuang;
import com.yanqu.road.server.manger.kuang.KuangMgr;
import com.yanqu.road.utils.property.PropertyHelper;

import java.util.Map;

public class KuangPb {

    public static Kuang.KuangUserDataTemp.Builder parseKuangUserDataTemp(KuangUserData userData) {
        if (userData == null) {
            return null;
        }
        Kuang.KuangUserDataTemp.Builder builder = Kuang.KuangUserDataTemp.newBuilder();
        builder.setDrawTimes(userData.getDrawTimes());
        // 身上装备
        for (KuangEquipmentData equipmentData : userData.getEquipmentDataMap().values()) {
            builder.addEquip(parseKuangEquipmentDataTemp(equipmentData));
        }
        // 待选择的装备
        for (KuangEquipmentData equipmentData : userData.getChooseEquipmentDataList()) {
            builder.addChooseEquip(parseKuangEquipmentDataTemp(equipmentData));
        }
        // 玩家属性
        for (Map.Entry<eKuangAttributeType, Long> entry : userData.getAttributeMap().entrySet()) {
            Kuang.AttributeDataTemp.Builder attributeDataTemp = parseAttributeDataTemp(entry.getKey(), entry.getValue());
            if (attributeDataTemp != null) {
                builder.addAttribute(attributeDataTemp);
            }
        }
        // 阵营id
        builder.setCampId(userData.getCampId());
        builder.setTotalDrawTimes(userData.getTotalDrawTimes());
        builder.setRound(userData.getRoundDay());
        builder.setFirstRoundScore(userData.getLastRoundScore());
        builder.setCurioTimes(userData.getCurioTimes());
        return builder;
    }

    public static Kuang.AttributeEnum parseAttributeEnum(eKuangAttributeType type) {
        return Kuang.AttributeEnum.valueOf(type.getValue());
    }

    public static Kuang.KuangEquipmentDataTemp.Builder parseKuangEquipmentDataTemp(KuangEquipmentData equipmentData) {
        if (equipmentData == null) {
            return null;
        }
        Kuang.KuangEquipmentDataTemp.Builder builder = Kuang.KuangEquipmentDataTemp.newBuilder();
        builder.setEquipmentId(equipmentData.getEquipmentId());
        builder.setType(equipmentData.getType());
        // 基础属性
        for (Map.Entry<eKuangAttributeType, Long> entry : equipmentData.getAttributeMap().entrySet()) {
            Kuang.AttributeDataTemp.Builder attributeDataTemp = parseAttributeDataTemp(entry.getKey(), entry.getValue());
            if (attributeDataTemp != null) {
                builder.addAttribute(attributeDataTemp);
            }
        }
        // 固有属性
        for (Map.Entry<eKuangAttributeType, Long> entry : equipmentData.getFixAttributeMap().entrySet()) {
            Kuang.AttributeDataTemp.Builder attributeDataTemp = parseAttributeDataTemp(entry.getKey(), entry.getValue());
            if (attributeDataTemp != null) {
                builder.addFixAttribute(attributeDataTemp);
            }
        }
        // 进阶属性
        for (Map.Entry<eKuangAttributeType, Long> entry : equipmentData.getAdvancedAttributeMap().entrySet()) {
            Kuang.AttributeDataTemp.Builder attributeDataTemp = parseAttributeDataTemp(entry.getKey(), entry.getValue());
            if (attributeDataTemp != null) {
                builder.addAdvancedAttribute(attributeDataTemp);
            }
        }
        // 装备总属性
        for (Map.Entry<eKuangAttributeType, Long> entry : equipmentData.getTotalAttributeMap().entrySet()) {
            Kuang.AttributeDataTemp.Builder attributeDataTemp = parseAttributeDataTemp(entry.getKey(), entry.getValue());
            if (attributeDataTemp != null) {
                builder.addTotalAttribute(attributeDataTemp);
            }
        }
        return builder;
    }

    public static Kuang.AttributeDataTemp.Builder parseAttributeDataTemp(eKuangAttributeType type, long value) {
        // 转一下枚举
        Kuang.AttributeEnum attributeEnum = KuangPb.parseAttributeEnum(type);
        if (attributeEnum == null) {
            return null;
        }
        Kuang.AttributeDataTemp.Builder builder = Kuang.AttributeDataTemp.newBuilder();
        builder.setType(attributeEnum);
        builder.setValue(value);
        return builder;
    }

    public static Kuang.KuangMatchGroupTemp.Builder parseKuangMatchGroupTemp(KuangMatchGroup matchGroup) {
        if (matchGroup == null) {
            return null;
        }
        Kuang.KuangMatchGroupTemp.Builder builder = Kuang.KuangMatchGroupTemp.newBuilder();
        builder.setWeekId(matchGroup.getWeekId());
        builder.setGroupId(matchGroup.getGroupId());
        builder.setOpenDay(matchGroup.getOpenDay());
        builder.addAllServerIdList(matchGroup.getServerIdList());
        builder.setCrossId(matchGroup.getCrossId());
        return builder;
    }

    public static KuangMatchGroup parseKuangMatchGroup(Kuang.KuangMatchGroupTemp temp) {
        KuangMatchGroup matchGroup = new KuangMatchGroup();
        matchGroup.setWeekId(temp.getWeekId());
        matchGroup.setGroupId(temp.getGroupId());
        matchGroup.setOpenDay(temp.getOpenDay());
        matchGroup.setServerIdList(temp.getServerIdListList());
        matchGroup.setCrossId(temp.getCrossId());
        return matchGroup;
    }

    public static Kuang.KuangRankRewardTemp.Builder parseKuangRankRewardTemp(KuangRankRewardConfig rankRewardConfig) {
        if (rankRewardConfig == null) {
            return null;
        }
        Kuang.KuangRankRewardTemp.Builder builder = Kuang.KuangRankRewardTemp.newBuilder();
        builder.setMinRank(rankRewardConfig.getMinRank());
        builder.setMaxRank(rankRewardConfig.getMaxRank());
        builder.setReward(rankRewardConfig.getReward());
        return builder;
    }

    public static Kuang.KuangLotteryRewardTemp.Builder parseKuangLotteryRewardTemp(PropertyWeightItem weightItem) {
        if (weightItem == null) {
            return null;
        }
        Kuang.KuangLotteryRewardTemp.Builder builder = Kuang.KuangLotteryRewardTemp.newBuilder();
        builder.setReward(PropertyHelper.parsePropertyToString(weightItem.getProperty()));
        builder.setWeight(weightItem.getWeight());
        return builder;
    }
}
